<template>
  <div id="AsnDetailList">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="selectData" @backList="backList" @del="del"
      @newCreate="newCreate"></v-tableTitle>
    <div class="detail">
      <component :data="{ detailData, argument }" :serialArr.sync="serialList" :is="currentComponent"></component>
    </div>

    <el-dialog :visible.sync="editModule.flag" :title="editModule.title" v-dialogDrag
      :close-on-click-modal="false" width="80%">
      <adjustmentEditor v-if="editModule.flag" :id="editModule.activeId" :data.sync="editModule.data" ref="edit">
      </adjustmentEditor>
      <div slot="footer" class="editor-footer">
        <el-button type="primary" @click="submitEditor">{{ $t("common.save") }}</el-button>
        <el-button @click="cancel">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import tableTitle from "commons/TableTitle";
import navMenu from "commons/NavMenu";
import baseMixin from "@/mixins/BaseMixin";
import config from "../Config"
import adjustmentEditor from "./page/edit.vue";

export default {
  name: "DetailList",
  mixins: [baseMixin, config],
  data() {
    return {
      newFlag: false,
      detailsNew: false,
      detailsFlag: false,
      argument: {},
      detailData: {},
      selectData: [],
      detailNav: "1",
      componentConfig: [
        {
          name: "Table",
          label: "table",
          component: () => import('./page/Table.vue')
        },
        {
          name: "GatherDetailDetail",
          label: this.$t('common.detail'),
          component: () => import('./page/particularsDetail.vue')
        },
      ],
      editModule: {
        title: "",
        flag: false,
        activeId: 0,
        data: null
      },
      serialList:[],
      isInventoryAdd:false,//是否为序列号库存复核》差异调整复核
    }
  },
  computed: {
    btnConfig() {
      if (this.detailNav == 1) {
        return [
          { label: this.$t('common.create'), certigier: this.detailNewStatus||!this.btnPermNameList.includes('WM06020409'), invalid: "newCreate" },
          { label: this.$t('common.delete'), certigier: this.deleteDetailStatus||!this.btnPermNameList.includes('WM06020410'), disabled: true, invalid: "del" },
        ]
      } else if (this.$store.state.newFlag) {
        return []
      } else {
        return [
          { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM06020411'), invalid: "backList" },
        ]
      }
    },
    currentComponent() {
      return this.componentConfig[this.detailNav - 1].component;
    }
  },
  methods: {
    submitEditor(){
      console.log(this.editModule.data);
      console.log(this.$refs.edit.serialList);
      this.editModule = {
        title: "",
        flag: false,
        activeId: 0,
        data: null
      }
    },
    cancel(){
      this.editModule = {
        title: "",
        flag: false,
        activeId: 0,
        data: null
      }
    },
    //删除数据
    async del(data) {
      try {
        let dataList = [];
        data.forEach((item) => {
          dataList.push({ adjustmentkey: item.adjustmentkey, adjustmentlinenumber:item.adjustmentlinenumber});
        })
        let res = await this.$api.post(`/deleteAdjustmentdetail`, dataList);
        
          this.$message.success(res.returnMgr);
          //this.$children[1].getTableData(1, {})
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.$children[1].$refs.table.delSelectData(data);
      }
    },
    //选中的数据
    setSelectData(data) {
      this.selectData = data;
    },
    //返回试图
    backList() {
      this.detailNav = "1";
      this.detailsFlag = false;
      this.detailsNew = false;
    },
    //新建
    async newCreate() {
      try {
        let res = await this.$api.get(`/getMaxAdjustmentlinenumber/${this.argument.adjustmentkey}`);
        
          this.detailData = {
            adjustmentlinenumber: res.bean,
            uom: "EA",
            packkey: "STD",
            qty: 0,
            casecnt: 0,
            innerpack: 0,
            pallet: 0,
            cube: 0,
            grosswgt: 0,
            netwgt: 0,
            otherunit1: 0,
            otherunit2: 0,
            itrnkey: " ",
            forte_Flag: "1",
            actualgrosswgt: 0,
            actualnetwgt: 0,
            actualtarewgt: 0,
            adjgrosswgt: 0,
            adjnetwgt: 0,
            adjtarewgt: 0,
            targetadj: 0,
            targetqty: 0,
            qtyselected: 0,
            lot: '',
            sku: '',
            loc: '',
          };
          this.detailsFlag = true;
          this.detailsNew = true;
          this.detailNav = "2";
        
      } catch (e) {
        this.$message.error(e);
      }

    },
    async setdialog(data) {
      try {
        let res = await this.$api.post(`/queryAdjustmentdetailByKey`, {
          adjustmentkey: this.argument.adjustmentkey,
          adjustmentlinenumber: data.adjustmentlinenumber
        });
        
          this.detailData = res.bean;
          this.detailsFlag = true;
          this.detailNav = "2";
        
      } catch (e) {
        this.$message.error(e);
      }
    },
  },
  async mounted() {
    
    this.newFlag = this.$store.state.newFlag;
    console.log(this.newFlag);

    if (this.newFlag) {
      this.detailData = {
        adjustmentlinenumber: "",
        uom: "EA",
        packkey: "STD",
        qty: 0,
        casecnt: 0,
        innerpack: 0,
        pallet: 0,
        cube: 0,
        grosswgt: 0,
        netwgt: 0,
        otherunit1: 0,
        otherunit2: 0,
        itrnkey: " ",
        forte_Flag: "1",
        actualgrosswgt: 0,
        actualnetwgt: 0,
        actualtarewgt: 0,
        adjgrosswgt: 0,
        adjnetwgt: 0,
        adjtarewgt: 0,
        targetadj: 0,
        targetqty: 0,
        qtyselected: 0,
        lot: "",
        sku: "",
        loc: "",
      };
      this.detailsFlag = true;
      this.detailsNew = true;
      this.detailNav = "2";
      if(this.$store.state.routeData.inventoryId){
        this.argument.serialkey = this.$store.state.routeData.inventoryId;
        this.$api.post(`/queryLotxlocid`,{serialkey:this.$store.state.routeData.inventoryId}).then(res => {
            const list = res.page.list;
            if(list.length > 0){
              const data = list[0];
              this.detailData.sku = data.sku;
              this.detailData.loc = data.loc;
              this.detailData.lot = data.lot;
              this.detailData.currentqty = data.qty;
              this.detailData.id = data.id;
              this.isInventoryAdd = true;
            }
          
        })
      }
    } else if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.argument = data.routeData;
      this.detailData = data.detailList;
      this.detailNav = data.detailListNav;
      this.detailsFlag = data.detailListFlag;
    } else {
      this.argument = this.$store.state.routeData;
    }

  },
  components: {
    "v-tableTitle": tableTitle,
    "v-navMenu": navMenu,
    adjustmentEditor
  },
  watch: {
    selectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteDetailStatus = false;
        } else {
          this.deleteDetailStatus = true;
        }
      }
    },
  }
}
</script>

<style>

</style>
